package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No3_TuiGuang.Adaoter.RGStaticsAdapter;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.R;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class ToBottomBusinessTempActivity extends BaseActivity {

    private static final int AD_EDIT_BOTTOM = 11;
    private  ViewPager vpStatisticSum;
    private TabLayout tab_title;
    private List<Fragment> fragmentList;
    private RGStaticsAdapter rGStaticsAdapter;
    private List<String> list_title;
    private ImageView back;
    private TextView ok;
    private BannnerTempFragment bannnerTempFragment;
    private BigBannnerTempFragment bigBannnerTempFragment;
    private List<AdBannerInfo> busList=new ArrayList<>();
    private List<AdBannerInfo> bigbusList=new ArrayList<>();
    private int bottomCount;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_to_bottom_business_temp;
    }

    @Override
    protected void initView() {
        vpStatisticSum = (ViewPager) findViewById(R.id.vpStatisticSum);
        tab_title = (TabLayout) findViewById(R.id.rank_tab_title);
        back =(ImageView)findViewById(R.id.back);
        ok = (TextView) findViewById(R.id.ok);
    }

    @Override
    protected void initData() {
        fragmentList = new ArrayList<>();
        bottomCount = getIntent().getIntExtra("bottomCount", 0);

        bannnerTempFragment =  new BannnerTempFragment();
        fragmentList.add(bannnerTempFragment);

        bigBannnerTempFragment =  new BigBannnerTempFragment();
        fragmentList.add(bigBannnerTempFragment);

        list_title = new ArrayList<>();
        list_title.add("普通广告");
        list_title.add("大图通栏");
        //设置TabLayout的模式
        tab_title.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tab_title.addTab(tab_title.newTab().setText(list_title.get(0)));
        tab_title.addTab(tab_title.newTab().setText(list_title.get(1)));

        rGStaticsAdapter = new RGStaticsAdapter(getSupportFragmentManager(),fragmentList, list_title);
        vpStatisticSum.setAdapter(rGStaticsAdapter);
        tab_title.setupWithViewPager(vpStatisticSum);
        registerListener();
    }

    private void registerListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                busList.clear();
                bigbusList.clear();
                if (bannnerTempFragment.adapter != null&&bigBannnerTempFragment.adapter!=null) {
                    if (bannnerTempFragment.adapter.getBussList() != null && bannnerTempFragment.adapter.getBussList().size() > 0) {
                        busList.addAll(bannnerTempFragment.adapter.getBussList());
                    }
                    if ((busList.size() + bottomCount) > 10) {
                        ToastUtils.show(ToBottomBusinessTempActivity.this, "最多再添加" + (10 - bottomCount) + "个广告模板");
                        return;
                    }
                    if (bigBannnerTempFragment.adapter.getBigBussList() != null && bigBannnerTempFragment.adapter.getBigBussList().size() > 0) {
                        bigbusList.addAll(bigBannnerTempFragment.adapter.getBigBussList());
                    }
                }

                Intent intent=new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("bigbottomList", (Serializable) busList);
                bundle.putSerializable("bigbusList",(Serializable)bigbusList);
                intent.putExtras(bundle);
                setResult(AD_EDIT_BOTTOM,intent);
                finish();
            }
        });
    }
}
